//
//  ThermometerView.m
//  UHealth
//
//  Created by liliang on 15/2/3.
//  Copyright (c) 2015年 gjtx. All rights reserved.
//

#import "ThermometerView.h"
#import "Masonry.h"

@interface ThermometerView ()

@property (nonatomic, strong)UIImageView            *bgImageView;
@property (nonatomic, strong)UIImageView            *dotImageView;

@end

@implementation ThermometerView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        UIImage *bgImage = [UIImage imageNamed:@"thermometer"];
        CGSize bgImageSize = bgImage.size;
        
//        self.bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(frame.size.width-bgImageSize.width-10, frame.size.height-bgImageSize.height-15, bgImageSize.width, bgImageSize.height)];
//        self.bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, bgImageSize.width, bgImageSize.height)];
        self.bgImageView = [[UIImageView alloc] init];
        self.bgImageView.image = bgImage;
//        self.bgImageView.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:0.5];
        self.bgImageView.clipsToBounds = YES;
//        self.bgImageView.center = CGPointMake(frame.size.width/2, frame.size.height/2+5);
        [self addSubview:self.bgImageView];
        
        [self.bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.center.equalTo(self);
            make.centerX.mas_equalTo(self.mas_centerX);
            make.bottom.equalTo(self.mas_bottom).with.offset(-8);
            make.top.greaterThanOrEqualTo(self.mas_top).with.offset(3);
            make.height.mas_equalTo(bgImageSize.height);
        }];
        
//        UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 14, 133)];
//        [self.bgImageView addSubview:bgView];
        UIView *bgView = [[UIView alloc] init];
        bgView.tag = 3000;
//        bgView.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:0.3];
        [self.bgImageView addSubview:bgView];
        
        [bgView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.bgImageView).with.offset(0);
            make.left.equalTo(self.bgImageView).with.offset(0);
            make.height.mas_equalTo(133);
            make.width.mas_equalTo(14);
        }];
        
        CALayer *maskLayer = [CALayer layer];
//        maskLayer.frame = bgView.bounds;
        maskLayer.frame = CGRectMake(0, 0, 14, 133);
        maskLayer.contents = (id)([UIImage imageNamed:@"mask"].CGImage);
        bgView.layer.mask = maskLayer;
        
//        self.dotImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 133, 14, 133)];
        self.dotImageView = [[UIImageView alloc] init];
        UIImage *image = [UIImage imageNamed:@"thermometer_pointer"];
        image = [image resizableImageWithCapInsets:UIEdgeInsetsMake(6, 0, 6, 0) resizingMode:UIImageResizingModeStretch];
        self.dotImageView.image = image;
        [bgView addSubview:self.dotImageView];
        
        
        [self.dotImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(bgView).with.offset(0);
            make.left.equalTo(bgView).with.offset(0);
            make.height.mas_equalTo(133);
            make.width.mas_equalTo(14);
        }];
    }
    return self;
}

- (void)setCurrentTemperature:(CGFloat)temperatureVale
{
    float tempValue = temperatureVale;
    UIView *bgView = [self.bgImageView viewWithTag:3000];
    if (tempValue < 35) {
        UIImage *image = [UIImage imageNamed:@"thermometer_pointer_blue"];
        image = [image resizableImageWithCapInsets:UIEdgeInsetsMake(6, 0, 6, 0) resizingMode:UIImageResizingModeStretch];
        self.dotImageView.image = image;
        [UIView animateWithDuration:1 animations:^{
            [self.dotImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(bgView).with.offset(133-7);
                make.left.equalTo(bgView).with.offset(0);
                make.height.mas_equalTo(133);
                make.width.mas_equalTo(14);
            }];
//            self.dotImageView.frame = CGRectMake(0, 133-7, 14, 133);
        }];
    }
    else if (tempValue > 40.5) {
        UIImage *image = [UIImage imageNamed:@"thermometer_pointer_red"];
        image = [image resizableImageWithCapInsets:UIEdgeInsetsMake(6, 0, 6, 0) resizingMode:UIImageResizingModeStretch];
        self.dotImageView.image = image;
        [UIView animateWithDuration:1 animations:^{
//            self.dotImageView.frame = CGRectMake(0, 0, 14, 133);
            [self.dotImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(bgView).with.offset(0);
                make.left.equalTo(bgView).with.offset(0);
                make.height.mas_equalTo(133);
                make.width.mas_equalTo(14);
            }];
        }];
    }
    else {
        float progressValue = (tempValue-35.0f)/(40.5-35.0f);
        float progressHeight = 115*progressValue;
        UIImage *image;
        if (temperatureVale >= 35 && temperatureVale <= 37) {
           image = [UIImage imageNamed:@"thermometer_pointer_green"];
        }
        else if (temperatureVale >37 & temperatureVale <=38) {
            image = [UIImage imageNamed:@"thermometer_pointer_yellow"];
        }
        else if (temperatureVale > 38) {
            image = [UIImage imageNamed:@"thermometer_pointer_red"];
        }
        image = [image resizableImageWithCapInsets:UIEdgeInsetsMake(6, 0, 6, 0) resizingMode:UIImageResizingModeStretch];
        self.dotImageView.image = image;
        [UIView animateWithDuration:1 animations:^{
//            self.dotImageView.frame = CGRectMake(0, 133-7-progressHeight, 14, 133);
            [self.dotImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(bgView).with.offset(133-7-progressHeight);
                make.left.equalTo(bgView).with.offset(0);
                make.height.mas_equalTo(133);
                make.width.mas_equalTo(14);
            }];
        }];
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
